package com.zhuzi.controller;

import com.zhuzi.controller.param.RestParam;
import com.zhuzi.dto.GatherFeedback;
import com.zhuzi.utils.XmlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author zhuzi
 * 2022/9/20 17:35
 */


@Slf4j
@org.springframework.web.bind.annotation.RestController
@RequestMapping("rest")
public class RestTempController {

    @Resource
    RestTemplate restTemplate;
    private String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<GATHER_FEEDBACK xmlns=\"http://rpb.customs.gov.cn/common/FrontEndSchemas/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
            "  <AREA_ID>CNCEK070127</AREA_ID>\n" +
            "  <CHNL_NO>CNCEK070127WI06</CHNL_NO>\n" +
            "  <SESSION_ID>CNCEK070127WI0620220915115520</SESSION_ID>\n" +
            "  <RELLIST_ID_TYPE>运输批次号</RELLIST_ID_TYPE>\n" +
            "  <FEEDBACK_TIME>2022-09-15T11:55:41.1410726+08:00</FEEDBACK_TIME>\n" +
            "  <CHECK_RESULT>N</CHECK_RESULT>\n" +
            "  <INSTRUCTION/>\n" +
            "  <PROC_ERROR_CODE>KKPE001</PROC_ERROR_CODE>\n" +
            "  <PROC_ERROR_DESCRIPTION>蒙AF820R, 未找到电子车牌E2801105200078D58FAF0AC0的备案信息！</PROC_ERROR_DESCRIPTION>\n" +
            "  <TECH_ERROR_CODE/>\n" +
            "  <TECH_ERROR_DESCRIPTION/>\n" +
            "  <VE_INFO>\n" +
            "    <VE_LICENSE_NO>蒙AF820R</VE_LICENSE_NO>\n" +
            "  </VE_INFO>\n" +
            "  <CONTA_ID/>\n" +
            "  <ESEAL_ID/>\n" +
            "  <OP_HINT>没有电子车牌的备案信息</OP_HINT>\n" +
            "  <LED_HINT>海关不放行</LED_HINT>\n" +
            "  <EXTENDED_CONTENT/>\n" +
            "</GATHER_FEEDBACK>";
    private String url = "http://localhost:8082/rest/info";

    @GetMapping("send")
    public String send() {

        RestParam restParam = new RestParam();
        restParam.setType("0x81").setContent(content);

        String s = restTemplate.postForObject(url, restParam, String.class);
        log.info("发送返回信息[{}]", s);

        return LocalDateTime.now().toString() + s;
    }

    @PostMapping("info")
    public RestParam receInfo(@RequestBody RestParam param) {
        log.info("接收到81报文[{}]", param);
        GatherFeedback gatherFeedback = XmlUtils.jacksonXML2Bean(param.getContent(), GatherFeedback.class);
        return new RestParam().setType("0x81").setContent(XmlUtils.jacksonBean2XML(gatherFeedback));
    }
}
